package com.right.health.app.ui.me;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.common.DataCommon;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.FamilyApi;

import java.util.List;

/*
 * Created by hopeliao on 2019-09-29
 *
 */
public class FamilyDetailActivity extends BaseActivity implements View.OnClickListener , OnHttpListener {

    private TextView mNameTv, mCardNoTv, mPhoneTv, mRelationshipTv;

    private FamilyResponse.FamilyInfo mResponse;

    private FamilyApi mFamilyApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.family_detail);
    }

    @Override
    public void findView() {
        findViewById(R.id.tv_add_visit_card).setOnClickListener(this);
        findViewById(R.id.tv_del).setOnClickListener(this);

        mNameTv = (TextView) findViewById(R.id.tv_name);
        mCardNoTv = (TextView) findViewById(R.id.tv_cardNo);
        mPhoneTv = (TextView) findViewById(R.id.tv_phone);
        mRelationshipTv = (TextView) findViewById(R.id.tv_relationship);
    }

    @Override
    public void initialize() {
        mFamilyApi = new FamilyApi();
        mFamilyApi.setListener(this);

        Bundle bundle = getIntent().getExtras();

        if(bundle != null) {
            mResponse  = bundle.getParcelable(BroadCastKey.DATA);
            if(mResponse != null) {
                if (!TextUtils.equals(mResponse.membership,"2")) {
                    mNameTv.setText(mResponse.membername);
                }else {
                    mNameTv.setText(App.getInstance().getUser().personname);
                }

                mCardNoTv.setText(mResponse.getShowCardNo());
                mPhoneTv.setText(mResponse.memberphone);

                try {
                    List<FamilyResponse.FamilyInfo> infos =  DataCommon.getFamilyRelationship();
                    for(FamilyResponse.FamilyInfo info:infos) {
                        if(info.membership.equals(mResponse.membership)) {
                            mRelationshipTv.setText(info.shipName);
                            break;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_add_visit_card: //添加就诊卡
                startIntent(MeVisitCardAddActivity.class);
                break;
            case R.id.tv_del: //删除
                AlertDialog dialog = DataCommon.getDeleteDialog(this, (dialog1, which) -> {
                    dialog1.dismiss();
                    if(mResponse != null) {
                        mFamilyApi.delMember(mResponse.memberid);
                    }
                });
                dialog.show();
                break;
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseOK()) {
            ToastUtil.show(this, "删除成功");
            sendBroadcast(BroadCastCode.REFRESH_FAMILY);
            finish();
        } else {
            ToastUtil.show(this, "删除失败,请稍后再试");
        }
    }
}
